package com.contactbook.web.client;


import com.contactbook.web.client.dto.AccountDTO;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class CreateAccountWidget extends View {
	
	private VerticalPanel content;
	
	private TextBox textBoxLogin;
	private PasswordTextBox textBoxPassword;
	private PasswordTextBox textBoxConfirmPassword;
	
	private final static String TITLE = "Create a new account"; 
	
	public CreateAccountWidget(ContactBook contactBook) {
		
		super(contactBook);
			
	}
	
	private class CancelHandler implements ClickHandler {
		public void onClick(ClickEvent event) {
			textBoxLogin.setText("");
			textBoxPassword.setText("");
			textBoxConfirmPassword.setText("");
		}
	}
	
	private class OkHandler implements ClickHandler {
		public void onClick(ClickEvent event) {
			String login = textBoxLogin.getText();
			String password = textBoxPassword.getText();
			String confirmPassword=  textBoxConfirmPassword.getText();
						
			if (!login.isEmpty() && !password.isEmpty() && password.equals(confirmPassword)) {
				AccountDTO accountDTO = new AccountDTO(login, password);
				final AsyncCallback<AccountDTO> callbackLogin = new AsyncCallback<AccountDTO>() {

					@Override
					public void onFailure(Throwable caught) {
						new DialogBoxWidget(contactBook, "error login");
					}

					@Override
					public void onSuccess(AccountDTO result) {
						contactBook.getCurrentSession().logon(result);
						DialogBoxWidget dialog = new DialogBoxWidget(contactBook, "Your account has been created! Now you can access to your personal contact book.");
						dialog.onHide("listOfContacts");						
					}
				};
				AsyncCallback<AccountDTO> callback = new AsyncCallback<AccountDTO>() {

					@Override
					public void onFailure(Throwable caught) {
						new DialogBoxWidget(contactBook, "error create "+caught.getMessage());
					}

					@Override
					public void onSuccess(AccountDTO result) {
						contactBook.getAccountService().login(result.getLogin(), result.getPassword(), callbackLogin);
					}
					
				};
				contactBook.getAccountService().create(accountDTO, callback);
			}
			else {
				new DialogBoxWidget(contactBook, "Assurez-vous que tous les champs soient remplis et que la confirmation du mot de passe est correcte.").show();
			}
		}
	}

	@Override
	public VerticalPanel getContent() {
		if (content == null)
			return buildView();
		return content;
	}
	
	public VerticalPanel buildView() {
		
		content = new VerticalPanel();
		
		DecoratorPanel panelCreateAccount = new DecoratorPanel();
		VerticalPanel panelWidgets = new VerticalPanel();
		
		panelWidgets.setSpacing(5);
		panelWidgets.setWidth(Constants.SmallPanelWidgetsStandartWidth);
		
		final FlexTable table = new FlexTable();
		
		final Label labelTitle = new Label("Create an account");
		labelTitle.setStyleName("gwt-label-labelnote");
		labelTitle.setHeight("30px");
		
		table.setWidget(0, 0, labelTitle);
		table.getFlexCellFormatter().setColSpan(0, 0, 2);
			
		final Label labelLogin = new Label("Login ");
		textBoxLogin = new TextBox();
		textBoxLogin.setFocus(true);
		
		table.setWidget(1, 0, labelLogin);
		table.setWidget(1, 1, textBoxLogin);
		
		final Label labelPassword = new Label("Password ");
		textBoxPassword = new PasswordTextBox();
		
		table.setWidget(2, 0, labelPassword);
		table.setWidget(2, 1, textBoxPassword);
		
		final Label labelConfirmPassword = new Label("Confirm password ");
		textBoxConfirmPassword = new PasswordTextBox();
		
		table.setWidget(3, 0, labelConfirmPassword);
		table.setWidget(3, 1, textBoxConfirmPassword);
		
		/* Buttons */
		final Button buttonOk = new Button("Ok");
		buttonOk.addClickHandler(new OkHandler());
		buttonOk.setStyleName("buttonform");
		
		final Button buttonCancel = new Button("Cancel");
		buttonCancel.addClickHandler(new CancelHandler());
		buttonCancel.setStyleName("buttonform");
		
		
		table.setWidget(4, 0, buttonOk);
		table.setWidget(4, 1, buttonCancel);

		/* Table properties */
		table.getFlexCellFormatter().setHeight(4, 0, "50px");
		
		table.getFlexCellFormatter().setAlignment(4, 0, ALIGN_CENTER, ALIGN_MIDDLE);
		table.getFlexCellFormatter().setAlignment(4, 1, ALIGN_CENTER, ALIGN_MIDDLE);
		
		table.setCellPadding(6);
		
		panelWidgets.add(table);
		panelCreateAccount.add(panelWidgets);
			
		content.add(panelCreateAccount);
		
		return content;
	}

	@Override
	public String getTitle() {
		return TITLE;
	}
}
